package com.zy.system.controller;

/**
 * @Author Gong
 * @Description
 * @dATE 2023/6/3 11:03
 **/
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.common.result.Result;
import com.zy.model.system.SysRole;
import com.zy.model.vo.AssginRoleVo;
import com.zy.model.vo.SysRoleQueryVo;
import com.zy.system.annotation.Log;
import com.zy.system.enums.BusinessType;
import com.zy.system.exception.ZyException;
import com.zy.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    //http://localhost:8800/admin/system/sysRole/findAll


    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = sysRoleService.getRolesByUserId(userId);
        return Result.ok(roleMap);
    }
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping( "batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        sysRoleService.removeByIds(ids );
        return Result.ok();
    }
    @Log(title = "角色管理",businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping( "save")
    public Result saveRole(@RequestBody SysRole sysRole) {
        boolean issuccess = sysRoleService.save( sysRole);
        if(issuccess) {
            return Result.ok( );
        }else {
            return Result.fail( );
        }
    }


    //3条件分页查询
    // page当前页limit每页记录数
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result findPageQueryRole(@PathVariable Long page,
                                    @PathVariable Long limit,
                                    SysRoleQueryVo sysRoleQueryVo) {
        Page<SysRole> pageParam = new Page<>(page, limit);//调用service方法
        IPage<SysRole> pageModel = sysRoleService.selectPage(pageParam, sysRoleQueryVo);//返回
        return Result.ok(pageModel);
    }
        /**
         * @Author GongXL
         * @Description  查询角色所有记录
         * @Date 11:23 2023/6/3
         * @Param []
         * @return java.util.List<com.zy.model.system.SysRole>
         **/
    @ApiOperation("查询所有记录")
    @GetMapping("/findAll")
    public Result findAll() {

        try{
            int x = 3/0;
        }catch (Exception e){
            throw new ZyException(2000,"自定义异常");
        }

        List<SysRole> roleList = sysRoleService.list();
        return Result.ok(roleList);
    }

    //2逻辑删除接口
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("逻辑删除接口")
    @DeleteMapping( "/remove/{id}")
    public Result removeRole(@PathVariable Long id) {
        boolean issuccess = sysRoleService.removeById(id);
        if(issuccess){
            return Result.ok();
        }else{
           return Result.fail();
        }
    }


    @ApiOperation("根据id查询角色")
    @GetMapping( "/findRoelById/{id}")
    public Result findRoelById(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);

    }

    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("更新角色")
    @PostMapping( "/update")
    public Result update(@RequestBody SysRole sysRole) {
        boolean isSuccess = sysRoleService.updateById(sysRole);
        if(isSuccess){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }


}